<template>
    <div>
      <!--      面包屑导航区-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>

<!--      卡片视图区-->
      <el-card>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <!-- <el-col :span="2">
            <el-button type="primary" @click="addGoodPage">添加商品</el-button>
          </el-col> -->
        </el-row>

<!--        table表格区域-->
        <el-table
          :data="goodsList"
          stripe
          border
          style="width: 100%">
          <el-table-column
            type="index"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="goodName"
            label="作品名称">
          </el-table-column>
          <el-table-column
            prop="price"
            label="作品价格(元)">
          </el-table-column>
          <el-table-column
            prop="businessName"
            label="所属商家">
          </el-table-column>
          <el-table-column
            prop="goodsCat"
            label="所属分类">
          </el-table-column>
          <!-- <el-table-column prop="add_time" label="创建时间">
            <template slot-scope="scope">
              {{scope.row.add_time | dateFormat}}
            </template>
          </el-table-column> -->
          <el-table-column
            prop="goodsDesc"
            label="作品描述">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGood(scope.row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>

<!--        分页区域-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>
    </div>
</template>

<script>
  export default {
    name: 'List',
    data() {
      return {
        queryInfo: {
          // query: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0
      }
    },
    created() {
      this.getGoodsList();
    },
    methods: {
      async getGoodsList() {
        const {data: res} = await this.$http.get(`goods/list`, {params: this.queryInfo});
        console.log(res);
        if (res.status !== 200) return this.$message.error('获取商品列表失败！');
        this.goodsList = res.page.records;
        this.total = res.page.total;
        // console.log(this.goodsList);
      },

      handleSizeChange(val) {
        this.queryInfo.pagesize = val;
        this.getGoodsList();
      },
      handleCurrentChange(val) {
        this.queryInfo.pagenum = val;
        this.getGoodsList();
      },
      async removeGood(id) {
         const confirmRes = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err);
        //如果确认删除，则confirmRes的返回值为 confirm
        //如果取消删除，则confirmRes的返回值为 cancel
        if (confirmRes === 'cancel') return this.$message.info('已取消删除');
        const {data: res} = await this.$http.post(`goods/delete?id=${id}`);
        if (res.status !== 200) return this.$message.error('删除失败');
        this.getGoodsList();
        this.$message.success('删除成功');
      },
      addGoodPage() {
        this.$router.push('goods/add')
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
